#include"Queue.h"
#include"BinaryTree.h"



void QueueInit(Que* q)
{
	assert(q);
	q->head = q->tail = NULL;
}

void QueueDestory(Que* q)
{
	assert(q);
	assert(q->head != NULL);
	while (q->head)
	{
		Queue* temp = q->head->next;
		free(q->head);
		q->head = temp;
	}
	q->tail = NULL;
}

Queue* BuyNode(QData x)
{
	Queue* newnode = (Queue*)malloc(sizeof(Queue));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void QueuePush(Que* q, QData x)
{
	assert(q);
	if ( q->head == NULL)
	{
		q->head = q->tail = BuyNode(x);
	}
	else
	{
		Queue* newnode = BuyNode(x);
		q->tail->next = newnode;
		q->tail = newnode;
	}
}

void QueuePop(Que* q)
{
	assert(q);
	assert(q->head);
	if (q->head == q->tail)
	{
		q->tail = NULL;
	}
	Queue* temp = q->head->next;
	free(q->head);
	q->head = temp;
}

bool QueueEmpty(Que* q)
{
	return q->head == NULL;
}

BTNode* QueueTop(Que* q)
{
	return q->head->data;
}

void QueuePrint(Que* q)
{
	assert(q);
	Queue* temp = q->head;
	while (temp)
	{
		printf("%c  ", temp->data);
		temp = temp->next;
	}
	printf("\n");
}
